﻿Imports System
Imports System.Runtime.InteropServices
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Compiler

Namespace System.Workflow.Activities
    <ComVisible(False)> _
    Friend NotInheritable Class StateInitializationValidator
        Inherits CompositeActivityValidator
        ' Methods
        Public Overrides Function Validate(ByVal manager As ValidationManager, ByVal obj As Object) As ValidationErrorCollection
            Dim validationErrors As ValidationErrorCollection = MyBase.Validate(manager, obj)
            Dim stateInitialization As StateInitializationActivity = [TryCast](obj,StateInitializationActivity)
            If (stateInitialization Is Nothing) Then
                Throw New ArgumentException(SR.GetString("Error_UnexpectedArgumentType", New Object() { GetType(StateInitializationActivity).FullName }), "obj")
            End If
            Dim parent As StateActivity = [TryCast](stateInitialization.Parent,StateActivity)
            If (parent Is Nothing) Then
                validationErrors.Add(New ValidationError(SR.GetError_StateInitializationParentNotState, &H606))
                Return validationErrors
            End If
            Dim activity3 As Activity
            For Each activity3 In parent.EnabledActivities
                Dim activity4 As StateInitializationActivity = [TryCast](activity3,StateInitializationActivity)
                If ((Not activity4 Is Nothing) AndAlso (Not activity4 Is stateInitialization)) Then
                    validationErrors.Add(New ValidationError(SR.GetError_MultipleStateInitializationActivities, &H604))
                    Exit For
                End If
            Next
            Me.ValidateSetStateInsideStateInitialization(stateInitialization, parent, validationErrors)
            If StateMachineHelpers.ContainsEventActivity(stateInitialization) Then
                validationErrors.Add(New ValidationError(SR.GetError_EventActivityNotValidInStateInitialization, &H603))
            End If
            Return validationErrors
        End Function

        Private Sub ValidateSetStateInsideStateInitialization(ByVal stateInitialization As StateInitializationActivity, ByVal state As StateActivity, ByVal validationErrors As ValidationErrorCollection)
            Me.ValidateSetStateInsideStateInitializationCore(stateInitialization, state, validationErrors)
        End Sub

        Private Sub ValidateSetStateInsideStateInitializationCore(ByVal compositeActivity As CompositeActivity, ByVal state As StateActivity, ByVal validationErrors As ValidationErrorCollection)
            Dim activity As Activity
            For Each activity In compositeActivity.EnabledActivities
                Dim activity2 As CompositeActivity = [TryCast](activity,CompositeActivity)
                If (Not activity2 Is Nothing) Then
                    Me.ValidateSetStateInsideStateInitializationCore(activity2, state, validationErrors)
                Else
                    Dim activity3 As SetStateActivity = [TryCast](activity,SetStateActivity)
                    If (((Not activity3 Is Nothing) AndAlso Not String.IsNullOrEmpty(activity3.TargetStateName)) AndAlso activity3.TargetStateName.Equals(state.QualifiedName)) Then
                        validationErrors.Add(New ValidationError(SR.GetError_InvalidTargetStateInStateInitialization, &H605))
                        Exit For
                    End If
                End If
            Next
        End Sub

    End Class
End Namespace

